﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class RemindAddWindow : Form
    {
        private ComboLogic comboBll;
        private MemberLogic memberBll;
        public T_Remind remind;

        public RemindAddWindow(string memberId)
        {
            InitializeComponent();
            comboBll = new ComboLogic();
            remind = new T_Remind();
            memberBll = new MemberLogic();

            if (memberId != null)
            {
                remind.MemberId = memberId;
                txtMember.Text = memberBll.Where<T_Member>(m => m.Id == memberId).FirstOrDefault().CardId;
            }

            cmbState.DisplayMember = "DisplayText";
            cmbState.ValueMember = "Value1";
            cmbState.DataSource = comboBll.GetComboByBuzType("RemindState");

            cmbCirType.DisplayMember = "DisplayText";
            cmbCirType.ValueMember = "Value1";
            cmbCirType.DataSource = comboBll.GetComboByBuzType("RemindType");

            cmbWeek.DisplayMember = "DisplayText";
            cmbWeek.ValueMember = "Value1";
            cmbWeek.DataSource = comboBll.GetComboByBuzType("WeekDay");

            cmbCirType.Location = new Point(107, 71);
            pnlByYear.Location = new Point(185, 67);
            pnlByYear.Visible = false;
            pnlByMonth.Location = new Point(185, 67);
            cmbWeek.Location = new Point(185, 71);


        }

        private void btnType_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("RemindTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;

            remind.TreeId = formTreeSelecter.TypeId;
            txtType.Text = formTreeSelecter.TypeText;
        }

        private void rbIsCir_CheckedChanged(object sender, EventArgs e)
        {
            dtpRemindTime.Visible = false;
            cmbCirType.Visible = true;
            pnlByYear.Visible = true;
        }

        private void rbOne_CheckedChanged(object sender, EventArgs e)
        {
            dtpRemindTime.Visible = true;
            pnlByYear.Visible = false;
            pnlByMonth.Visible = false;
            cmbWeek.Visible = false;
            cmbCirType.Visible = false;
        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_Load(object sender, EventArgs e)
        {

        }

        private void cmbCirType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbCirType.SelectedValue.ToString() == "每年")
            {
                pnlByYear.Visible = true;
                pnlByMonth.Visible = false;
                cmbWeek.Visible = false;
                List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>();
                for (int i = 1; i <= 12; i++)
                {
                    list.Add(new KeyValuePair<int, int>(i, i));
                }

                cmbMonth.DisplayMember = "Key";
                cmbMonth.ValueMember = "Value";
                cmbMonth.DataSource = list;
            }
            else if (cmbCirType.SelectedValue.ToString() == "每月")
            {
                pnlByYear.Visible = false;
                pnlByMonth.Visible = true;
                cmbWeek.Visible = false;

                List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>();
                for (int i = 1; i <= 31; i++)
                {
                    list.Add(new KeyValuePair<int, int>(i, i));
                }


                cmbDay2.DisplayMember = "Key";
                cmbDay2.ValueMember = "Value";
                cmbDay2.DataSource = list;

            }
            else if (cmbCirType.SelectedValue.ToString() == "每周")
            {
                pnlByYear.Visible = false;
                pnlByMonth.Visible = false;
                cmbWeek.Visible = true;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtType.Text))
            {
                FormSysMessage.ShowMessage("请选择类型！");
                return;
            }

            if (remind.Id == null)
            {
                remind.Id = Guid.NewGuid().ToString("D");
                remind.IsCir = rbIsCir.Checked == true ? "是" : "否";
                remind.State = cmbState.Text;
                remind.Type = rbIsCir.Checked == true ? "循环提醒" : "一次性提醒";
                remind.CirType = cmbCirType.SelectedValue.ToString();
                remind.Remind = txtContent.Text;
                remind.AddTime = dtpAddTime.Value;

                if (rbIsCir.Checked)
                {
                    switch (cmbCirType.SelectedValue.ToString())
                    {
                        case "每年":
                            remind.R_Month = Convert.ToInt32(cmbMonth.SelectedValue);
                            remind.R_Day = Convert.ToInt32(cmbDay.SelectedValue);
                            var remindByYear = new DateTime(DateTime.Now.Year, remind.R_Month, remind.R_Day);
                            remindByYear = remindByYear < DateTime.Now.Date == true ? remindByYear.AddYears(1) : DateTime.Now.Date;
                            remind.NextRemindTime = remindByYear;
                            break;
                        case "每月":
                            remind.R_Day = Convert.ToInt32(cmbDay2.SelectedValue);
                            DateTime remindByMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, remind.R_Day);
                            remindByMonth = remindByMonth <= DateTime.Now.Date == true ? remindByMonth.AddMonths(1) : DateTime.Now.Date;
                            remind.NextRemindTime = remindByMonth;
                            break;
                        case "每周":
                            remind.R_WeekDay = Convert.ToInt32(cmbWeek.SelectedValue);
                            var remindByWeek = DateTime.Now.Date;
                            int num = (int)remindByWeek.DayOfWeek;
                            if (remindByWeek.DayOfWeek == DayOfWeek.Sunday)
                            {
                                num = 7;
                            }
                            if (num <= remind.R_WeekDay)
                            {
                                remindByWeek = remindByWeek.AddDays(remind.R_WeekDay - num);
                            }
                            else
                            {
                                var beforeWeek = remindByWeek.AddDays(remind.R_WeekDay - num);
                                remindByWeek = beforeWeek.AddDays(7);
                            }

                            remind.NextRemindTime = remindByWeek;
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    remind.NextRemindTime = dtpRemindTime.Value;
                }
                memberBll.AddRemind(remind);
            }
            else
            {

            }
            this.Close();
        }

        private void cmbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            int days = 30;
            switch (Convert.ToInt32(cmbMonth.SelectedValue))
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31;
                    break;
                case 2:
                    days = 28;
                    break;
                default:
                    break;
            }

            List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>();
            for (int i = 1; i <= days; i++)
            {
                list.Add(new KeyValuePair<int, int>(i, i));
            }

            cmbDay.DisplayMember = "Key";
            cmbDay.ValueMember = "Value";
            cmbDay.DataSource = list;
        }
    }
}
